﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using DataLayer.Controller;

public partial class provider_StateAccount : cGlobal
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //this.loadGrid("890903937-00");            
            if (Request.Params["NIT"] != null)
            {
                this.hdnNIT.Value = Request.Params["NIT"].ToString();
                this.loadGrid(this.hdnNIT.Value.ToString());
            }
        }
    }

    private void loadGrid(string nit)
    {
        DataTable dtt = ProviderController.GetPSECredito(nit);
        grvFactura.DataSource = dtt;
        grvFactura.DataBind();

        //Se guarda en session la tabla de los resultados, para poder filtrar
        Session["VIEWSTATEACCOUNT"] = dtt;
    }

    protected void grvFactura_RowDataBound(object sender, Obout.Grid.GridRowEventArgs e)
    {
        if (e.Row.RowType == Obout.Grid.GridRowType.DataRow)
        {
            HyperLink lnkVerDocumento = (HyperLink)e.Row.Cells[6].Controls[0].Controls[0].Controls[0].Controls[0].Controls[1];            
            lnkVerDocumento.NavigateUrl = "Javascript:openShadowbox('" + ConfigurationManager.AppSettings["urlPortal"] + "/provider/DocSupport.aspx?NoFactura=" + DataBinder.Eval(e.Row.DataItem, "NUMERO_FACTURA").ToString().Trim() + "','iframe','Documentos de Soporte',590,400);";

            HyperLink lnkVerFactura = (HyperLink)e.Row.Cells[7].Controls[0].Controls[0].Controls[0].Controls[0].Controls[1];
            lnkVerFactura.NavigateUrl = ConfigurationManager.AppSettings["urlFactura"].ToString() + DataBinder.Eval(e.Row.DataItem, "NUMERO_FACTURA").ToString().Trim() + "_" + DateTime.Parse(DataBinder.Eval(e.Row.DataItem, "FECHA_FACTURA").ToString()).ToString("ddMMyyyy") + ".pdf";
            //lnkVerFactura.NavigateUrl = "Javascript:openShadowbox('" + ConfigurationManager.AppSettings["urlPortal"] + "/provider/DocSupport.aspx?NoFactura=" + DataBinder.Eval(e.Row.DataItem, "NUMERO_FACTURA").ToString().Trim() + "','iframe','Factura',590,400);";
        }
    }

    protected void btnHistory_Click(object sender, ImageClickEventArgs e)
    {
        //Response.Redirect(urlPortal + "provider/HistoryPayment.aspx?NIT=" + Request.Params["NIT"].ToString() + "&usrNombre=" + Request.Params["usrNombre"].ToString());
        Response.Redirect(urlPortal + "provider/HistoryPayment.aspx?NIT=" + this.hdnNIT.Value.ToString() + "&usrNombre=xxx");
    }

    #region "Filtros de la grilla"

    protected void btnBuscarGrilla_Click(object sender, ImageClickEventArgs e)
    {
        if (string.IsNullOrEmpty(this.txtContiene.Text.Trim()))
            return;

        if (Session["VIEWSTATEACCOUNT"] != null)
        {
            DataTable dtt = (DataTable)Session["VIEWSTATEACCOUNT"];
            string columna = this.hdnColumnaSeleccionada.Value.ToString();

            DataView dw = dtt.DefaultView;
            dw.RowFilter = columna + " LIKE '%" + this.txtContiene.Text.Trim() + "%'";
            dtt = dw.ToTable();

            this.grvFactura.DataSource = dtt;
            this.grvFactura.DataBind();
            Session["VIEWSTATEACCOUNT"] = dtt;

            this.txtContiene.Text = string.Empty;
        }
    }

    protected void lnkAscendente_Click(object sender, EventArgs e)
    {
        if (Session["VIEWSTATEACCOUNT"] != null)
        {
            DataTable dtt = (DataTable)Session["VIEWSTATEACCOUNT"];
            string columna = this.hdnColumnaSeleccionada.Value.ToString();

            DataView dw = dtt.DefaultView;
            dw.Sort = columna + " ASC";
            dtt = dw.ToTable();

            this.grvFactura.DataSource = dtt;
            this.grvFactura.DataBind();
            Session["VIEWSTATEACCOUNT"] = dtt;
        }
    }

    protected void lnkDescendente_Click(object sender, EventArgs e)
    {
        if (Session["VIEWSTATEACCOUNT"] != null)
        {
            DataTable dtt = (DataTable)Session["VIEWSTATEACCOUNT"];
            string columna = this.hdnColumnaSeleccionada.Value.ToString();

            DataView dw = dtt.DefaultView;
            dw.Sort = columna + " DESC";
            dtt = dw.ToTable();

            this.grvFactura.DataSource = dtt;
            this.grvFactura.DataBind();
            Session["VIEWSTATEACCOUNT"] = dtt;
        }
    }

    protected void lnkSinFiltro_Click(object sender, EventArgs e)
    {
        this.loadGrid(this.hdnNIT.Value.ToString());
    }

    #endregion



}
